package cc.wsyw126.java.collection.map;

import java.util.*;

/**
 * Created by junpeng.yang on 16/8/26.
 */
public class MapCompareTwoWay {
    public HashMap<User, Integer> getMap(int n) {
        HashMap<User, Integer> map = new HashMap<>();
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < n; i++) {
            map.put(new User(random.nextInt(n)+1,"name"+random.nextInt(n)), random.nextInt(n));
        }
        return map;
    }

    /**
     * 通过提供一个带有比较的treemap将当前的map转换为有序的map
     */
    public void Method1() {
        HashMap<User, Integer> map = getMap(100);
        System.out.println("map.size() = " + map.size());
        map.keySet().forEach(n -> System.out.print(n.age + "  "));
        System.out.println();
        TreeMap<User, Integer> treeMap = sortMapByKey(map);
        System.out.println("treeMap.size() = " + treeMap.size());
        treeMap.keySet().forEach(n -> System.out.print(n.age + "  "));
    }

    private TreeMap<User, Integer> sortMapByKey(HashMap<User, Integer> map) {
        if (map == null)
            return null;
        TreeMap<User, Integer> sortMap = new TreeMap<>((user1,user2) -> user1.age-user2.age);
        sortMap.putAll(map);
        Integer u = sortMap.put(new User(0, "qq"), 112);
        Integer u1=  sortMap.put(new User(0, "qq12121212"), 12);
        Integer u2=  sortMap.put(new User(0, "qq1212"), 1212);
        System.out.println("u = " + u);
        System.out.println("u1 = " + u1);
        System.out.println("u2 = " + u2);
        System.out.println(sortMap.get(new User(0,"12")));
        return sortMap;
    }

    public static void main(String[] args) {
        MapCompareTwoWay mapCompareTwoWay = new MapCompareTwoWay();
        mapCompareTwoWay.Method1();
        System.out.println("\n------------------------------------------");

        mapCompareTwoWay.Method2();
    }

    private class User {
        int age;
        String name;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public User(int age, String name) {
            this.age = age;
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            User user = (User) o;

            if (age != user.age) return false;
            return !(name != null ? !name.equals(user.name) : user.name != null);

        }

        @Override
        public int hashCode() {
            int result = age;
            result = 31 * result + (name != null ? name.hashCode() : 0);
            return result;
        }
    }


    public void Method2(){
        HashMap<User, Integer> map = getMap(100);
        System.out.println("map.size() = " + map.size());
        ArrayList<Map.Entry<User,Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, (o1, o2) -> o1.getKey().getAge() - o2.getKey().getAge());
        System.out.println("list.size = " + list.size());
                list.forEach(n -> System.out.print("  " + n.getKey().getAge()));
    }

}
